package cn.ssdcc.dkserver.biz;

import cn.ssdcc.dkserver.entity.DkPath;
import cn.ssdcc.dkserver.entity.Music;
import cn.ssdcc.dkserver.entity.MusicGroup;
import cn.ssdcc.dkserver.entity.User;
import cn.ssdcc.dkserver.util.IsNullUtil;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
@Slf4j
public class UserBiz extends CommonBiz{

    @PostConstruct
    public void init() {
        try {
            File dir = new File(DkPath.DataPath+"/user");
            if(!dir.exists()){
                dir.mkdirs();//创建目录
                log.info("文件夹"+DkPath.DataPath+"/user"+"创建成功");
            }
        } catch (Exception e) {
            log.error("文件夹"+DkPath.DataPath+"/user"+"创建失败",e);
        }
        try {
            File dir = new File(DkPath.DataPath+"/im");
            if(!dir.exists()){
                dir.mkdirs();//创建目录
                log.info("文件夹"+DkPath.DataPath+"/im"+"创建成功");
            }
        } catch (Exception e) {
            log.error("文件夹"+DkPath.DataPath+"/im"+"创建失败",e);
        }
    }

    public User getUserMsgByDeviceid(String deviceid){
        List<User> users;
        List<String> deviceids = userMapper.selectUserDeviceidByOthertDeviceid(deviceid);
        if (deviceids.size()>0){
            deviceid = deviceids.get(0);
            users = userMapper.selectUserByDeviceid(deviceid);
            if (users.size()>0){
                return users.get(0);
            }
        }
        users = userMapper.selectUserByDeviceid(deviceid);
        if (users.size()>0){
            return users.get(0);
        }
        userMapper.insertNewUser(deviceid);
        users = userMapper.selectUserByDeviceid(deviceid);
        return users.get(0);
    }

    public String buildPath(String dirPath, String filename){
        String[] types = filename.split("\\.");
        String path = dirPath+ System.currentTimeMillis() +"."+types[types.length-1];
        return path;
    }
}
